﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Entities;
using C.Unite;
using ChanExt;
using MySoft.Data;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_BASICDView : CommonPageBase
    {
        #region Inti
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBaseHelper.BindTree(treeMenu, EnumData.DataBaseEunm.Material, "");


                // BindGrid(GetWhere());
            }
        }
        #endregion

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.Title = "行政物资列表 - " + treeMenu.SelectedNode.Text;
            Grid1.RecordCount = TbsDbSession.Default.From<Adm_BASICD>().Where(wc).Count();

            List<Adm_BASICD> items = TbsDbSession.Default.From<Adm_BASICD>().Where(wc).OrderBy(Adm_BASICD._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (treeMenu.SelectedNodeID != "")
            {
                wc = wc && Adm_BASICD._.BATYPE == int.Parse(treeMenu.SelectedNodeID);
            }
            return wc;
        }

        #endregion

        protected void treeMenu_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            BindGrid(GetWhere());
        }

        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            string no = Grid1.Rows[e.RowIndex].Values[0].ToString();
            string id = Grid1.Rows[e.RowIndex].Values[1].ToString();
            string unit = Grid1.Rows[e.RowIndex].Values[2].ToString();
            if (string.IsNullOrEmpty(Request["t"]))
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(no, no + " - " + id) + ActiveWindow.GetHideReference());
            else
                PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(no + " - " + id) + ActiveWindow.GetHideReference());
        }
    }
}